package top.test;

import org.apache.ibatis.datasource.pooled.PooledDataSource;

import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.mybatis.generator.api.MyBatisGenerator;

import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import top.douma.services.AA;
import top.mybatis.entitys.Dish;
import top.mybatis.entitys.User;
import top.mybatis.mapper.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author Administrator
 * @version 1.0
 * @email lzjg1314@163.com
 * @date 2018/10/31
 */
@Service
public class Main {
  
  @Resource
  UserMapper userMapper;
  
  public static void main(String[] args) {
  
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
    AA main= (AA) applicationContext.getBean("top.douma.services.AA");
  
    main.regAndLog();
    
  }
  
  public static void useMybatis() {
    PooledDataSource dataSource = new PooledDataSource();
    dataSource.setDriver("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test");
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    configuration.addMapper(UserMapper.class);
    configuration.addMapper(BoardMapper.class);
    configuration.addMapper(DishTypeMapper.class);
    configuration.addMapper(OrderItemMapper.class);
    configuration.addMapper(OrderMapper.class);
    configuration.addMapper(DishMapper.class);
    //endregion
    
    
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    SqlSession session = sqlSessionFactory.openSession();
    
    UserMapper user = session.getMapper(UserMapper.class);
    List<User> users = user.selectAll();
    System.out.println(users.toString());
    
    DishMapper dish = session.getMapper(DishMapper.class);
    List<Dish> dishs = dish.selectAll();
    System.out.println(dishs.toString());
  }
  
  public static void generator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
    
    String generatorConfigPath = "generatorConfig.xml";
    String jdbcProperties = "jdbc.properties";
    boolean overwrite = true;
    List<String> warnings = new ArrayList<String>();
    
    //加载自动生成配置文件
    InputStream configFile = Main.class.getResourceAsStream(generatorConfigPath);
    
    //加载jdbc配置文件
    Properties extraProperties = new Properties();
    extraProperties.load(Main.class.getResourceAsStream(jdbcProperties));
    
    ConfigurationParser cp = new ConfigurationParser(extraProperties, warnings);
    org.mybatis.generator.config.Configuration config = cp.parseConfiguration(configFile);
    
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    
    myBatisGenerator.generate(null);
  }
  
  
  public void myBatis() {
    
  /*
    // 直接获取配置文件
    InputStream inputStream = Main.class.getResourceAsStream("/mybatis-config.xml");
    
    
    // 通过配置文件获取一个数据库 连接池
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession session = sqlSessionFactory.openSession();
    List<User> users = session.selectList("top.douma.UserMapper.sss");
    for (User user : users) {
      System.out.println(user.toString());
    }*/
    
    //1. mybaits 自动生成
    //   mybaits 自动生成工具包  org.mybatis.generator
    //2. mybatis + spring mvc
    
    
   /* //region 通过代码创建配置文件
    PooledDataSource dataSource = new PooledDataSource();
    dataSource.setDriver("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test");
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    configuration.addMapper(UserMapper.class);
    configuration.addMapper(BoardMapper.class);
    //endregion
    
    
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    SqlSession session = sqlSessionFactory.openSession();
    
    UserMapper mapper = session.getMapper(UserMapper.class);
    List<Board> lists = mapper.bb();
    for (Board list : lists) {
      System.out.println(list.toString());
    }*/
    /*User user1=new User();
    user1.setLogin("xxxxx");
    user1.setPassword("abcxxxx");
    //int count = session.insert("top.douma.UserMapper.add",user1);
    UserMapper mapper = session.getMapper(UserMapper.class);
    int count= mapper.update(9,user1);
    //int count= mapper.delete(11);
    System.out.println(count);
  
    // 事务
    session.commit();
    
    List<User> users = session.selectList("top.douma.UserMapper.sss");
    for (User user : users) {
      System.out.println(user.toString());
    }*/
  }
  
  
}
